// pages/user/user-invite/index.js
const app = getApp()
import {
  request
} from "../../../utils/util";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    painting: {},
    shareImage: '',
    sharePost: false,
    savePhotosAlbum: false,
    list: [],
    page: 1,
    size: 12,
    reachBottom: false,
    invitation_num: 0,
    commission_sum: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.loadData()
  },
  bindscrolltolower() {
    console.log('到底啦~')
    this.loadData(this.data.page + 1)
  },
  preventTouchMove: function () {},
  // 请求邀请记录
  loadData(page) {
    !this.data.reachBottom && request('/api/user/invitation', app.globalData.token, 'get', {
      page: page || 1,
      size: this.data.size
    }).then(res => {
      if (res.code === 200) {
        let reachBottom = false
        if (page && res.data.data.length < this.data.size) {
          reachBottom = true
          wx.showToast({
            title: '已经到底了哦~',
            icon: 'none'
          })
        }
        let list = this.data.list
        if (page) {
          list = [...list, ...res.data.data]
        } else {
          list = res.data.data
        }

        this.setData({
          list,
          page: page || 1,
          reachBottom,
          invitation_num: res.data.invitation_num,
          commission_sum: res.data.commission_sum
        })
      }
    })
  },
  // 关闭分享弹出层
  closeShare() {
    this.setData({
      sharePost: false,
      painting: {},
      shareImage: ''
    })
  },
  // 绘制生成分享图片
  eventDraw(e) {


    // app.globalData.login().then(res => {
    if (app.globalData.is_new === 2) {

      request('/api/user/fxQrcode', app.globalData.token, 'get').then(async res => {
        console.log(res.data, 'fxQrcode')
        let bgc = await request('/api/index/config', app.globalData.token, 'get')
        console.log(bgc,'bgc')
        const {
          avatarUrl,
          nickName
        } = app.globalData.userInfo
        // console.log(avatarUrl, 'avatarUrl')
        // console.log(nickName, 'nickName')
        wx.showLoading({
          title: '绘制分享图片中',
          mask: true,
          duration: 2000,
        })
        this.setData({
          sharePost: true,
          painting: {
            width: 375,
            height: 555,
            clear: true,
            views: [{
                type: 'rect',
                background: '#F6F6F6',
                width: 375,
                height: 555,
                top: 0,
                left: 0
              },
              {
                type: 'image',
                url: avatarUrl,
                top: 15,
                left: 30,
                width: 70,
                height: 70
              },
              {
                type: 'image',
                url: '/public/img/avatar_wrapper.png',
                top: 14.9,
                left: 29.8,
                width: 70.2,
                height: 70.2
              },
              {
                type: 'text',
                content: nickName,
                fontSize: 16,
                color: '#FE7C68',
                textAlign: 'left',
                top: 30,
                left: 120,
                bolder: true
              },
              {
                type: 'text',
                content: '邀请你来易为教育一起提升大专本科学历',
                fontSize: 12,
                color: '#563D20',
                textAlign: 'left',
                top: 60,
                left: 120
              },
              {
                type: 'rect',
                background: '#fff',
                width: 300,
                height: 365,
                top: 100,
                left: 35
              },
              {
                type: 'image',
                url: bgc.data && bgc.data.wxapp_poster_background? bgc.data.wxapp_poster_background:'/public/img/share_banner.png',
                top: 115,
                left: 50,
                width: 270,
                height: 280
              }, {
                type: 'text',
                content: '你的职责是平整土地,而非焦虑时光,你做三',
                fontSize: 12,
                color: '#333333',
                textAlign: 'left',
                top: 415,
                left: 55,
              },
              {
                type: 'text',
                content: '四月的事,在八九月自有答案。',
                fontSize: 12,
                color: '#333333',
                textAlign: 'left',
                top: 437,
                left: 55,
              },
              {
                type: 'image',
                url: '/public/img/share_head1@2x.png',
                top: 478,
                left: 55,
                width: 30,
                height: 30
              },

              {
                type: 'image',
                url: '/public/img/share_head2@2x.png',
                top: 478,
                left: 68,
                width: 30,
                height: 30
              },

              {
                type: 'image',
                url: '/public/img/share_head3@2x.png',
                top: 478,
                left: 83,
                width: 30,
                height: 30
              },

              {
                type: 'text',
                content: '易为教育  提升学历的不二选择',
                fontSize: 12,
                color: '#333333',
                textAlign: 'left',
                top: 518,
                left: 55,
              },

              {
                type: 'image',
                url: res.data !== '1' ? res.data : 'https://img.kerry.top/user_qrcode.png',
                top: 470,
                left: 255,
                width: 75,
                height: 75
              }
            ]
          }
        })
      })
    } else {
      wx.showToast({
        title: '请先授权用户信息',
        icon: 'none'
      })
    }

  },
  // 保存生成的海报到用户本地
  eventSave() {
    wx.getSetting({
      withSubscriptions: true,
      success: (res) => {
        console.log(res.authSetting['scope.writePhotosAlbum'])
        if (res.authSetting['scope.writePhotosAlbum'] !== false) {

          wx.saveImageToPhotosAlbum({
            filePath: this.data.shareImage,
            success(res) {
              console.log(res, 'saveImageToPhotosAlbum');
              wx.showToast({
                title: '保存图片成功',
                icon: 'success',
                duration: 2000
              })
            }
          })
        } else {
          // console.log(11);
          this.setData({
            savePhotosAlbum: true
          })
        }
      }
    })


  },

  openSettingTap() {
    const that = this
    if (!this.data.savePhotosAlbum) return
    console.log(222);

    wx.openSetting({
      withSubscriptions: true,
      success(res) {
        console.log(res)
        that.setData({
          savePhotosAlbum: !res.authSetting['scope.writePhotosAlbum']
        })
      },
      fail: err => {
        console.log(err)
      }
    })

  },

  eventGetImage(event) {
    console.log(event)
    wx.hideLoading()
    const {
      tempFilePath,
      errMsg
    } = event.detail
    if (errMsg === 'canvasdrawer:ok') {
      this.setData({
        shareImage: tempFilePath
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})